Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

504
Visualizações
When i try to fetch data in my React project, then I get this error: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

I want to get filtered data from MongoDb using a specific email in my react project. So, I create an HTTP request using an email and then try to use the data, then I get the following error:

GET http://localhost:5000/bloodRequest/amin@gmail.com 404 (Not Found) Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

My client-side requesting code:

  useEffect(() => {
    fetch(`http://localhost:5000/bloodRequest/amin@gmail.com`)
      .then((res) => res.json())
      .then((data) => setRequests(data));   }, [requests]);

My server-side code:

  app.get("/donateBlood/:email", async (req, res) => {
      const email = req.params.email;
      const query = { email: email };
      const cursor = donateBloodsCollection.find(query);
      const result = await cursor.toArray();
      res.json(result);
    });

But when I change the HTTP request syntax and use the email in the middle of the HTTP link, it works fine. example:

Client-side code:

 useEffect(() => {
    fetch(`http://localhost:5000/amin@gmail.com/bloodRequest`)
      .then((res) => res.json())
      .then((data) => setRequests(data));   }, [requests]);

server-side code:

app.get("/:email/donateBlood", async (req, res) => {
      const email = req.params.email;
      const query = { email: email };
      const cursor = donateBloodsCollection.find(query);
      const users = await cursor.toArray();
      res.json(users);
    });

In that case, it works properly, I don't know why? Can you explain, please?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The fetch call is calling a URL /bloodRequest/amin@gmail.com, the corresponding server API definition has /donateBlood/:email, so the 404 error you have got is expected.

GET http://localhost:5000/bloodRequest/amin@gmail.com 404 (Not Found) Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

Try either changing the client-side URL to /donateBlood/amin@gmail.com or server-side API to /bloodRequest/:email.

PS: Basically, the code you are trying is syntactically valid. It must have been failed due to typos in the URL or not restarting the server after a particular change. I can see the same URL mismatch in the working code you have posted, so it should also have thrown 404. Please post the exact code you have tried if it doesn't work yet.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda